package com.htu.courseService.dictCode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htu.courseService.common.enums.ErrorCodeEnum;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.exception.BusinessException;
import com.htu.courseService.dictCode.dao.DictCodeMapper;
import com.htu.courseService.dictCode.entity.DictCode;
import com.htu.courseService.dictCode.service.DictCodeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class DictCodeServiceImpl implements RedisCachePrefix, DictCodeService {
    private final DictCodeMapper dictCodeMapper;

    @Override
    public IPage<DictCode> getPage(Integer pageNum,Integer pageSize,String dictionaryCode) {
        Page<DictCode> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<DictCode> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(dictionaryCode)){
            wrapper.eq(DictCode::getDictionaryCode,dictionaryCode);
        }
        wrapper.eq(DictCode::getStatus,"active");
        IPage<DictCode> dictCodeList = dictCodeMapper.selectPage(page,wrapper);
        return dictCodeList;
    }

    @Override
    public void insertDictCode(DictCode dictCode) {
        LambdaQueryWrapper<DictCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictCode::getDictionaryCode,dictCode.getDictionaryCode());
        wrapper.eq(DictCode::getStatus,"active");
        wrapper.orderByDesc(DictCode::getSortOrder);
        wrapper.last("limit 1");
        DictCode dictCodeOne = dictCodeMapper.selectOne(wrapper);
        if(Objects.isNull(dictCodeOne)){
            dictCode.setSortOrder(1);
        }else {
            dictCode.setSortOrder(dictCodeOne.getSortOrder() + 1);
        }
        dictCode.setCreateTime(new Date());
        int count = dictCodeMapper.insert(dictCode);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.INSERT_FAIL);
        }
    }

    @Override
    public void deleteDictCode(Long id) {
        int count = dictCodeMapper.deleteById(id);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.DELETE_FAIL);
        }
    }

    @Override
    public void updateDictCode(DictCode dictCode) {
        int count = dictCodeMapper.updateById(dictCode);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.UPDATE_FAIL);
        }
    }

    @Override
    public List<DictCode> getList(String dictionaryCode) {
        LambdaQueryWrapper<DictCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictCode::getDictionaryCode,dictionaryCode);
        wrapper.eq(DictCode::getStatus,"active");
        List<DictCode> dictCodeList = dictCodeMapper.selectList(wrapper);
        return dictCodeList;
    }
}
